﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace _1241118_DoAn_hqtcsdl
{
    public partial class Ban_Ve : Form
    {
        string connect = @"Data Source=QUAN\QUAN1504;Initial Catalog=QuanLyGaSG;Integrated Security=True";

        public Ban_Ve()
        {
            InitializeComponent();
            
            //   comboBox15.Visible = false;
        }


        // Nút  NHẬP HÀNH KHÁCH 

        private void button7_Click(object sender, EventArgs e)
        {
            dataGridView4.Visible = false;
            if (textBox2.Text != "" && comboBox2.Text != "" && textBox4.Text != "" && textBox6.Text != "" && textBox5.Text != "" && comboBox11.Text != "")
            {

                SqlConnection a = new SqlConnection(connect);
                a.Open();

                string sqlcomman = "INSERT_HANHKHACH";
                SqlCommand sqlcm2 = new SqlCommand(sqlcomman, a);
                sqlcm2.CommandType = CommandType.StoredProcedure;

                sqlcm2.Parameters.Add("@Ten", SqlDbType.NVarChar, 50).Value = textBox2.Text.ToString();
                sqlcm2.Parameters.Add("@GTinh", SqlDbType.NVarChar, 50).Value = comboBox2.Text.ToString();
                sqlcm2.Parameters.Add("@cmnd", SqlDbType.NVarChar, 50).Value = textBox4.Text.ToString();
                sqlcm2.Parameters.Add("@adr", SqlDbType.NVarChar, 50).Value = textBox6.Text.ToString();
                sqlcm2.Parameters.Add("@dt", SqlDbType.NVarChar, 50).Value = textBox5.Text.ToString();
                sqlcm2.Parameters.Add("@slv", SqlDbType.Int, 50).Value = Convert.ToInt32(comboBox11.Text);
                sqlcm2.ExecuteNonQuery();
                sqlcm2.Dispose();
                MessageBox.Show("Thông Tin quý khách đã được lưu.");

                Connectdatabase b = new Connectdatabase();
                SqlCommand sqlcm3 = b.connectdatabase("SELECT * FROM HANHKHACH ");

                SqlDataAdapter sqlcm4 = new SqlDataAdapter(sqlcm3);
                DataTable data3 = new DataTable();
                sqlcm4.Fill(data3);
                dataGridView1.DataSource = data3;

                // Show Mã HANH KHACH
                Connectdatabase a3 = new Connectdatabase();
                SqlCommand sqlcm23 = a3.connectdatabase("select MaHKhach  from HANHKHACH where CMND = '" + textBox4.Text + "'");
                SqlDataAdapter sqldt23 = new SqlDataAdapter(sqlcm23);
                DataTable data33 = new DataTable();
                sqldt23.Fill(data33);
                comboBox13.DataSource = data33;
                comboBox13.DisplayMember = "MaHKhach";
                comboBox13.ValueMember = "MaHKhach";

                // Xem lại danh sách HÀNH KHÁCH
                dataGridView1.Visible = false;
                
                dataGridView2.Visible = true;
                Connectdatabase a1 = new Connectdatabase();
                SqlCommand sqlcm21 = a1.connectdatabase("SELECT * FROM HANHKHACH ");

                SqlDataAdapter sqlcm31 = new SqlDataAdapter(sqlcm21);
                DataTable data31 = new DataTable();
                sqlcm31.Fill(data31);
                dataGridView2.DataSource = data31;
            }
            else
            {
                MessageBox.Show("Điền đầy đủ thông tin để thêm  hành khách .", "Thông Báo");
            }
            return;

        }




        // rê chuột dataGri 1 hứng trên các ô trên

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

            
            textBox7.Text = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
            comboBox4.Text = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
            comboBox9.Text = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();

            Connectdatabase a2 = new Connectdatabase();
            SqlCommand sqlcm22 = a2.connectdatabase("select * from ThongTinChuyenTau where MaTau = '" + textBox7.Text + "' and MaChuyen ='" + comboBox4.Text + "'");
            SqlDataAdapter sqldt22 = new SqlDataAdapter(sqlcm22);
            DataTable data30 = new DataTable();
            sqldt22.Fill(data30);
            comboBox6.DataSource = data30;
            comboBox6.DisplayMember = "MaLC";
            comboBox6.ValueMember = "MaLC";

            Connectdatabase a4 = new Connectdatabase();
            SqlCommand sqlcm24 = a4.connectdatabase("select * from ThongTinToa where MaTau = '" + textBox7.Text + "' and MaChuyen ='" + comboBox4.Text + "'");
            SqlDataAdapter sqldt24 = new SqlDataAdapter(sqlcm24);
            DataTable data32 = new DataTable();
            sqldt24.Fill(data32);
            comboBox5.DataSource = data32;
            comboBox5.DisplayMember = "MaToa";
            comboBox5.ValueMember = "MaToa";

            comboBox7.DataSource = data32;
            comboBox7.DisplayMember = "STTToa";
            comboBox7.ValueMember = "STTToa";

            Connectdatabase a3 = new Connectdatabase();
            SqlCommand sqlcm23 = a3.connectdatabase("select * from ThongTinGhe where MaTau = '" + textBox7.Text + "' and MaChuyen = '" + comboBox4.Text + "' and MaToa = '" + comboBox5.Text + "'  and STTToa='" + comboBox7.Text + "'and TinhTrang ='Yes'");
            SqlDataAdapter sqldt23 = new SqlDataAdapter(sqlcm23);
            DataTable data33 = new DataTable();
            sqldt23.Fill(data33);
            comboBox8.DataSource = data33;
            comboBox8.DisplayMember = "STTGhe";
            comboBox8.ValueMember = "STTGhe";

           

            


            
           
        }


        // Nút TÍNH GIÁ VÉ

        private void button3_Click(object sender, EventArgs e)
        {
            if (comboBox1.Text != "" && comboBox11.Text != "" && comboBox6.Text != "" && comboBox5.Text != "" && textBox11.Text != "")
            {
                dataGridView2.Visible = false;
                dataGridView4.Visible = false;
                string gio = DateTime.Now.ToString(); // gio hiện tại 
                SqlConnection a1 = new SqlConnection(connect);
                a1.Open();

                string sqlcomman = "USP_TINHTIENVE";
                SqlCommand sqlcm21 = new SqlCommand(sqlcomman, a1);
                sqlcm21.CommandType = CommandType.StoredProcedure;
                sqlcm21.Parameters.Add("@DIADIEMDI", SqlDbType.NVarChar, 50).Value = comboBox1.Text.ToString();
                sqlcm21.Parameters.Add("@SOLUONG", SqlDbType.Int, 50).Value = Convert.ToInt32(comboBox11.Text);
                sqlcm21.Parameters.Add("@LOAICHUYEN", SqlDbType.NVarChar, 50).Value = comboBox6.Text.ToString();
                sqlcm21.Parameters.Add("@LOAIGHE", SqlDbType.NVarChar, 50).Value = comboBox5.Text.ToString();
                sqlcm21.Parameters.Add("@MADOITUONG", SqlDbType.NVarChar, 50).Value = textBox11.Text.ToString();
  //              sqlcm21.ExecuteNonQuery();
                sqlcm21.Dispose();

                SqlDataAdapter x = new SqlDataAdapter(sqlcm21);
                DataTable datax = new DataTable();
                x.Fill(datax);
                comboBox15.DataSource = datax;
                comboBox15.DisplayMember = "GIAIN";
                comboBox15.ValueMember = "GIAIN";
            }
            else
            {
                MessageBox.Show("Chưa đủ điều kiện tính tiền vé ");
            }

        }



        //load  Mô tả đối tượng lên ô comboBox3
        private void Ban_Ve_Load(object sender, EventArgs e)
        {
           
            timer1.Start();
            //// không cho ghi vào các comboBox
           
            this.comboBox16.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboBox3.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboBox2.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboBox14.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboBox12.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
      
            
         

            Connectdatabase a3 = new Connectdatabase();
            SqlCommand sqlcm23 = a3.connectdatabase("select * from MoTaDoiTuong ");
            SqlDataAdapter sqldt23 = new SqlDataAdapter(sqlcm23);
            DataTable data33 = new DataTable();
            sqldt23.Fill(data33);
            comboBox3.DataSource = data33;
            comboBox3.DisplayMember = "MoTaDT";
            comboBox3.ValueMember = "MoTaDT";

            Connectdatabase a2 = new Connectdatabase();
            SqlCommand sqlcm22 = a2.connectdatabase("select * from DanhSachGa ");
            SqlDataAdapter sqldt22 = new SqlDataAdapter(sqlcm22);
            DataTable data32 = new DataTable();
            sqldt22.Fill(data32);
            comboBox1.DataSource = data32;
            comboBox1.DisplayMember = "MoTaGa";
            comboBox1.ValueMember = "MoTaGa";

            //// Load lên tổng số vé có thể mua
            Connectdatabase a1 = new Connectdatabase();
            SqlCommand sqlcm1 = a2.connectdatabase("select distinct (STTGhe) from ThongTinGhe order by  STTGhe ");

            SqlDataAdapter sqldt1 = new SqlDataAdapter(sqlcm1);
            DataTable data1 = new DataTable();
            sqldt1.Fill(data1);
            comboBox11.DataSource = data1;
            comboBox11.DisplayMember = "STTGhe";
            comboBox11.ValueMember = "STTGhe";
          

        }



        // Nút XÓA 1 HÀNH KHÁCH
        private void button5_Click(object sender, EventArgs e)
        {
            dataGridView1.Visible = false;
            dataGridView4.Visible = false;
            dataGridView5.Visible = false;
            if (comboBox13.Text != "")
            {
                Connectdatabase a = new Connectdatabase();
                SqlCommand sqlcm2 = a.connectdatabase("DELETE HanhKhach WHERE MaHKhach = '" + comboBox13.Text + "'");
                sqlcm2.ExecuteNonQuery();
                MessageBox.Show("Thông Tin quý khách đã được XÓA.");

                // Xem lại danh sách
                Connectdatabase a1 = new Connectdatabase();
                SqlCommand sqlcm21 = a1.connectdatabase("SELECT * FROM HANHKHACH ");

                SqlDataAdapter sqlcm31 = new SqlDataAdapter(sqlcm21);
                DataTable data31 = new DataTable();
                sqlcm31.Fill(data31);
                dataGridView2.DataSource = data31;

            }
            else
            {
                MessageBox.Show("Điền Mã Hành Khách Vào", "Thông Báo");
            }
        }



        // show đối tượng bên bản đối tượng
        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            



            Connectdatabase z = new Connectdatabase();
            SqlCommand sqlcmz = z.connectdatabase("select GiamGia from MoTaDoiTuong where MoTaDT = N'" + comboBox3.Text + "'");
            string giamgia = null;// lấy giá trị trong bảng ra              
            giamgia = sqlcmz.ExecuteScalar().ToString();
            sqlcmz.Dispose();
            string giamgia2 = Convert.ToString(giamgia);         
            textBox10.Text = giamgia2.ToString();






            Connectdatabase a19 = new Connectdatabase();
            SqlCommand sqlcm219 = a19.connectdatabase("Select MaDT from MoTaDoiTuong where MoTaDT = N'" + comboBox3.Text + "'");
            string DT = null;
            DT = sqlcm219.ExecuteScalar().ToString();
            sqlcm219.Dispose();
            textBox11.Text = DT;


            // tính luôn giá vé khi thay đôi comboBox
            //  Tính giá vé dùng proc
            if (comboBox1.Text != "" && comboBox11.Text != "" & comboBox6.Text != "" && comboBox15.Text != "" && textBox11.Text != "")
            {
                SqlConnection a1 = new SqlConnection(connect);
                a1.Open();

                string sqlcomman = "USP_TINHTIENVE";
                SqlCommand sqlcm21 = new SqlCommand(sqlcomman, a1);
                sqlcm21.CommandType = CommandType.StoredProcedure;
                sqlcm21.Parameters.Add("@DIADIEMDI", SqlDbType.NVarChar, 50).Value = comboBox1.Text.ToString();
                sqlcm21.Parameters.Add("@SOLUONG", SqlDbType.Int, 50).Value = Convert.ToInt32(comboBox11.Text);
                sqlcm21.Parameters.Add("@LOAICHUYEN", SqlDbType.NVarChar, 50).Value = comboBox6.Text.ToString();
                sqlcm21.Parameters.Add("@LOAIGHE", SqlDbType.NVarChar, 50).Value = comboBox5.Text.ToString();
                sqlcm21.Parameters.Add("@MADOITUONG", SqlDbType.NVarChar, 50).Value = textBox11.Text.ToString();
        //        sqlcm21.ExecuteNonQuery();
                sqlcm21.Dispose();
                SqlDataAdapter x = new SqlDataAdapter(sqlcm21);
                DataTable datax = new DataTable();
                x.Fill(datax);
                comboBox15.DataSource = datax;
                comboBox15.DisplayMember = "GIAIN";
                comboBox15.ValueMember = "GIAIN";
                
            }

        }

        // XEM LẠI DANH SÁCH BIÊN LAI

        private void button2_Click(object sender, EventArgs e)
        {
            dataGridView4.Visible = false;
            dataGridView1.Visible = false;
            dataGridView2.Visible = false;
            dataGridView3.Visible = true;
            dataGridView5.Visible = false;
            //   //  Xem lại danh sách biên lai
            Connectdatabase a1 = new Connectdatabase();
            SqlCommand sqlcm21 = a1.connectdatabase("SELECT * FROM BIENLAI ");

            SqlDataAdapter sqlcm31 = new SqlDataAdapter(sqlcm21);
            DataTable data31 = new DataTable();
            sqlcm31.Fill(data31);
            dataGridView3.DataSource = data31;


            // có delay

            //SqlConnection a1 = new SqlConnection(connect);
            //a1.Open();
            //string sqlcomman = "SHOWTHONGTINBIENLAI";
            //SqlCommand sqlcm21 = new SqlCommand(sqlcomman, a1);
            //sqlcm21.ExecuteNonQuery();
            //sqlcm21.Dispose();
            //SqlDataAdapter x = new SqlDataAdapter(sqlcm21);
            //DataTable datax = new DataTable();
            //x.Fill(datax);
            //dataGridView3.DataSource = datax;


            //Xóa các thông tin trên textBox
            textBox1.ResetText();
            textBox1.ResetText();
            textBox12.ResetText();
            textBox7.ResetText();
            textBox2.ResetText();
            textBox4.ResetText();
            textBox6.ResetText();
            textBox6.ResetText();
            comboBox3.ResetText();
            comboBox4.ResetText();
            comboBox5.ResetText();
            comboBox6.ResetText();
            comboBox7.ResetText();
            comboBox8.ResetText();
            comboBox9.ResetText();
            comboBox13.ResetText();


        }


        // Nút xem toàn bộ Hành Khách 

        private void XemHK_Click(object sender, EventArgs e)
        {
            dataGridView1.Visible = false;
            dataGridView3.Visible = false;
            dataGridView2.Visible = true;
            dataGridView4.Visible = false;
            dataGridView5.Visible = false;
            Connectdatabase a = new Connectdatabase();
            SqlCommand sqlcm2 = a.connectdatabase("SELECT * FROM HANHKHACH ");

            SqlDataAdapter sqlcm3 = new SqlDataAdapter(sqlcm2);
            DataTable data3 = new DataTable();
            sqlcm3.Fill(data3);
            dataGridView2.DataSource = data3;

            // show thong tin hanh khach co delay


            //SqlConnection a1 = new SqlConnection(connect);
            //a1.Open();
            //string sqlcomman = "USP_SHOWINFOKHACHHANG";
            //SqlCommand sqlcm21 = new SqlCommand(sqlcomman, a1);
            //sqlcm21.CommandType = CommandType.StoredProcedure;
            ////sqlcm21.ExecuteNonQuery();
            //sqlcm21.Dispose();
            //SqlDataAdapter x = new SqlDataAdapter(sqlcm21);
            //DataTable datax = new DataTable();
            //x.Fill(datax);
            //dataGridView2.DataSource = datax;




        }


        // hứng data từ DataGriView 2
        private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            textBox2.Text = dataGridView2.Rows[e.RowIndex].Cells[1].Value.ToString();
            comboBox13.Text = dataGridView2.Rows[e.RowIndex].Cells[0].Value.ToString();
            comboBox2.Text = dataGridView2.Rows[e.RowIndex].Cells[2].Value.ToString();
            textBox4.Text = dataGridView2.Rows[e.RowIndex].Cells[3].Value.ToString();
            textBox6.Text = dataGridView2.Rows[e.RowIndex].Cells[4].Value.ToString();
            textBox5.Text = dataGridView2.Rows[e.RowIndex].Cells[5].Value.ToString();
            comboBox11.Text = dataGridView2.Rows[e.RowIndex].Cells[6].Value.ToString();

        }


        // Hiện thị giá vé  NIÊM YẾT khi thay đổi nơi đến
        //private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        //{
   
        //}


        // XÓA 1 BIÊN LAI - trả ve- insert vô bảng trả đổi vé
        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                dataGridView1.Visible = false;
                dataGridView2.Visible = false;
                dataGridView3.Visible = true;
                dataGridView5.Visible = false;

                if (textBox1.Text != "")
                {
                    // insert vào bảng trả đổi vé trước khi xóa 1 biên lai
                    if (textBox2.Text != "" && comboBox1.Text != "" && comboBox6.Text != "" && textBox12.Text != "" &&
                        comboBox9.Text != "" && textBox9.Text != "" && textBox4.Text != "" && textBox6.Text != "" && comboBox12.Text != "")
                    {
                        SqlConnection x = new SqlConnection(connect);
                        x.Open();
                        string sqlcommanx = "INSERT_TraVe";
                        SqlCommand sqlcmx = new SqlCommand(sqlcommanx, x);
                        sqlcmx.CommandType = CommandType.StoredProcedure;
                        sqlcmx.Parameters.Add("@mv", SqlDbType.NVarChar, 50).Value = textBox1.Text.ToString();
                        sqlcmx.Parameters.Add("@ten", SqlDbType.NVarChar, 50).Value = textBox2.Text.ToString();
                        sqlcmx.Parameters.Add("@ddd", SqlDbType.NVarChar, 50).Value = comboBox1.Text.ToString();
                        sqlcmx.Parameters.Add("@lc", SqlDbType.NVarChar, 50).Value = comboBox6.Text.ToString();
                        sqlcmx.Parameters.Add("@timemua", SqlDbType.DateTime).Value = textBox12.Text;
                        sqlcmx.Parameters.Add("@giodi", SqlDbType.DateTime).Value = comboBox9.Text;
                        sqlcmx.Parameters.Add("@timetra", SqlDbType.DateTime).Value = textBox9.Text;
                        sqlcmx.Parameters.Add("@cmnd", SqlDbType.NVarChar, 50).Value = textBox4.Text.ToString();
                        sqlcmx.Parameters.Add("@dc", SqlDbType.NVarChar, 50).Value = textBox6.Text.ToString();
                        sqlcmx.Parameters.Add("@ld", SqlDbType.Int, 50).Value = Convert.ToInt32(comboBox12.Text);
                        sqlcmx.ExecuteNonQuery();
                        sqlcmx.Dispose();
                        
                    }
                    else
                    {
                        MessageBox.Show("Điền đầy đủ thông tin.");
                        return;
                    }
            

                    MessageBox.Show("Trả vé thành công ");
                    // xóa 1 dong trong biên lai
                    Connectdatabase a = new Connectdatabase();
                    SqlCommand sqlcm2 = a.connectdatabase("DELETE BienLai WHERE MaVe = '" + textBox1.Text + "'");
                    sqlcm2.ExecuteNonQuery();
                    //MessageBox.Show("1 Biên lai đã XÓA.");


                    // Xem lại danh sách
                    Connectdatabase a1 = new Connectdatabase();
                    SqlCommand sqlcm21 = a1.connectdatabase("SELECT * FROM BienLai ");

                    SqlDataAdapter sqlcm31 = new SqlDataAdapter(sqlcm21);
                    DataTable data31 = new DataTable();
                    sqlcm31.Fill(data31);
                    dataGridView3.DataSource = data31;



                    //Cập nhật lại tình trạng ghế sau khi bán xong vé
                    int slve = Convert.ToInt32(comboBox11.Text);
                    if (slve == 1)
                    {
 
                        Connectdatabase a2 = new Connectdatabase();
                        SqlCommand sqlcm22 = a2.connectdatabase("update ThongTinGhe set TinhTrang = 'Yes' Where  MaTau = '" + textBox7.Text + "' and MaChuyen = '" + comboBox4.Text + "' and MaToa = '" + comboBox5.Text + "'  and STTToa='" + comboBox7.Text + "' and STTGhe='" + comboBox8.Text + "'  ");
                        sqlcm22.ExecuteNonQuery();
                    }
                    else
                    {
                        int soghe = Convert.ToInt32(comboBox8.Text);
                        for ( int i = 0 ; i < slve ; i++)
                        {
                            Connectdatabase a3 = new Connectdatabase();
                            SqlCommand sqlcm23 = a3.connectdatabase("update ThongTinGhe set TinhTrang = 'Yes' Where  MaTau = '" + textBox7.Text + "' and MaChuyen = '" + comboBox4.Text + "' and MaToa = '" + comboBox5.Text + "'  and STTToa='" + comboBox7.Text + "' and STTGhe = '"+soghe+"' ");
                            sqlcm23.ExecuteNonQuery();
                            soghe++;
                        }
                    }
                    
                    
                    
                    
                    // Cập nhật lại số lượng ghế trong toa đó
                    Connectdatabase z = new Connectdatabase();
                    SqlCommand sqlcmz = z.connectdatabase("select GheConLai from ThongTinToa Where  MaTau = '" + textBox7.Text + "' and MaChuyen = '" + comboBox4.Text + "' and MaToa = '" + comboBox5.Text + "'  and STTToa='" + comboBox7.Text + "'");
                    string gheconlai;//= null;// lấy giá trị trong bảng ra
                    gheconlai = sqlcmz.ExecuteScalar().ToString();
                    sqlcmz.Dispose();
                    int gheconlai2 = Convert.ToInt32(gheconlai);
                    int soghemoi = 0;
                    int slv = Convert.ToInt32(comboBox11.Text);
                    soghemoi = gheconlai2 + slv;
                    textBox8.Text = soghemoi.ToString();
                    Connectdatabase z1 = new Connectdatabase();
                    SqlCommand sqlcmz1 = z1.connectdatabase("update ThongTinToa set GheConLai = '" + textBox8.Text + "' Where  MaTau = '" + textBox7.Text + "' and MaChuyen = '" + comboBox4.Text + "' and MaToa = '" + comboBox5.Text + "'  and STTToa='" + comboBox7.Text + "' ");
                    sqlcmz1.ExecuteNonQuery();

                    // reset lại giá trị  Mã Vé
                    textBox1.ResetText();
                    textBox1.ResetText();
                    textBox12.ResetText();
                    textBox7.ResetText();
                    textBox2.ResetText();
                    textBox6.ResetText();
                    textBox4.ResetText();
                    textBox8.ResetText();
                    comboBox3.ResetText();
                    comboBox4.ResetText();
                    comboBox5.ResetText();
                    comboBox6.ResetText();
                    comboBox7.ResetText();
                    comboBox8.ResetText();
                    comboBox9.ResetText();
                    comboBox15.ResetText();
                    comboBox13.ResetText();



                }
                else
                {
                    dataGridView1.Visible = false;
                    dataGridView3.Visible = false;
                    MessageBox.Show("Điền Mã Vé Vào", "Thông Báo");
                }
            }
            catch (Exception a)
            {
                MessageBox.Show(a.Message + "Không được quyền trả vé.");
                return;
            }
            
        }


        // hứng data từ dataGri 3
        private void dataGridView3_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            textBox1.Text = dataGridView3.Rows[e.RowIndex].Cells[0].Value.ToString();
            textBox12.Text = dataGridView3.Rows[e.RowIndex].Cells[14].Value.ToString();
            comboBox13.Text = dataGridView3.Rows[e.RowIndex].Cells[1].Value.ToString();
            textBox7.Text = dataGridView3.Rows[e.RowIndex].Cells[2].Value.ToString();
            comboBox4.Text = dataGridView3.Rows[e.RowIndex].Cells[3].Value.ToString();
            comboBox6.Text = dataGridView3.Rows[e.RowIndex].Cells[4].Value.ToString();
            comboBox5.Text = dataGridView3.Rows[e.RowIndex].Cells[5].Value.ToString();
            comboBox7.Text = dataGridView3.Rows[e.RowIndex].Cells[6].Value.ToString();
            comboBox8.Text = dataGridView3.Rows[e.RowIndex].Cells[7].Value.ToString();
            comboBox9.Text = dataGridView3.Rows[e.RowIndex].Cells[8].Value.ToString();
            textBox2.Text = dataGridView3.Rows[e.RowIndex].Cells[11].Value.ToString();
            comboBox3.Text = dataGridView3.Rows[e.RowIndex].Cells[12].Value.ToString();
            comboBox11.Text = dataGridView3.Rows[e.RowIndex].Cells[15].Value.ToString();

            //load CMND, Địa Chỉ lên 
            Connectdatabase a3 = new Connectdatabase();
            SqlCommand sqlcm23 = a3.connectdatabase("select DiaChi from HanhKhach where MaHKhach = '" + comboBox13.Text + "'");
            String diachi = null;
            diachi = sqlcm23.ExecuteScalar().ToString();
            sqlcm23.Dispose();
            textBox6.Text = diachi;

            SqlCommand sqlcm2 = a3.connectdatabase("select CMND from HanhKhach where MaHKhach = '" + comboBox13.Text + "'");
            String cmnd = null;
            cmnd = sqlcm2.ExecuteScalar().ToString();
            sqlcm2.Dispose();
            textBox4.Text = cmnd;

            //SqlCommand sqlcm = a3.connectdatabase("select SoLuongVe from HanhKhach where MaHKhach = '" + comboBox13.Text + "'");
            //String slv = null;
            //slv = sqlcm.ExecuteScalar().ToString();
            //sqlcm.Dispose();
            //comboBox11.Text = slv;

            
            




        }



        // Nút đặt vé, inseret vào biên lai
        private void button8_Click(object sender, EventArgs e)
        {
            dataGridView1.Visible = false;
            dataGridView2.Visible = false;
            dataGridView3.Visible = false;
            dataGridView4.Visible = false;
            dataGridView5.Visible = false;
            int slve = Convert.ToInt32(comboBox11.Text);
            int ghetrong = Convert.ToInt32(textBox8.Text);
            if (slve > ghetrong)
            {
                MessageBox.Show("Không đủ ghế để bán !","Thông Báo");
                return;
            }

            try
            {

                if (textBox2.Text != "" && comboBox15.Text != "")
                {
                    Connectdatabase a19 = new Connectdatabase();
                    SqlCommand sqlcm219 = a19.connectdatabase("Select TinhTrang from ThongTinGhe where MaTau = '" + textBox7.Text + "' and MaChuyen = '" + comboBox4.Text + "' and MaToa = '" + comboBox5.Text + "'  and STTToa='" + comboBox7.Text + "' and STTGhe='" + comboBox8.Text + "' ");
                    string tinhtrang = null;
                    tinhtrang = sqlcm219.ExecuteScalar().ToString();
                    sqlcm219.Dispose();
                    if (tinhtrang == "No")  // Sử dụng NO chính xác, do trong CSDL chưa có dữ liệu  nên để yes để test thử 
                    {
                        MessageBox.Show(" Ghế này đã được bán. Vui lòng chọn số ghế khác.", "Thông Báo");
                        return;
                    }
                    else
                    {
                        SqlConnection x = new SqlConnection(connect);
                        x.Open();
                        string sqlcommanx = "INSERT_BIENLAI";
                        string abx = "N";
                        SqlCommand sqlcmx = new SqlCommand(sqlcommanx, x);
                        sqlcmx.CommandType = CommandType.StoredProcedure;

                        sqlcmx.Parameters.Add("@MaHK", SqlDbType.NVarChar, 50).Value = comboBox13.Text.ToString();
                        sqlcmx.Parameters.Add("@MTau", SqlDbType.NVarChar, 50).Value = textBox7.Text.ToString();
                        sqlcmx.Parameters.Add("@MC", SqlDbType.NVarChar, 50).Value = comboBox4.Text.ToString();
                        sqlcmx.Parameters.Add("@MLC", SqlDbType.NVarChar, 50).Value = comboBox6.Text.ToString();
                        sqlcmx.Parameters.Add("@MToa", SqlDbType.NVarChar, 50).Value = comboBox5.Text.ToString();
                        sqlcmx.Parameters.Add("@STToa", SqlDbType.Int, 50).Value = Convert.ToInt32(comboBox7.Text);
                        sqlcmx.Parameters.Add("@SoGhe", SqlDbType.Int, 50).Value = Convert.ToInt32(comboBox8.Text);
                        sqlcmx.Parameters.Add("@GioDi", SqlDbType.DateTime).Value = comboBox9.Text;
                        sqlcmx.Parameters.Add("@DDI", SqlDbType.NVarChar, 50).Value = "SG";//comboBox12.Text.ToString();
                        sqlcmx.Parameters.Add("@DDD", SqlDbType.NVarChar, 50).Value = comboBox1.Text.ToString();
                        sqlcmx.Parameters.Add("@TenHK", SqlDbType.NVarChar, 50).Value = textBox2.Text.ToString();
                        sqlcmx.Parameters.Add("@MDT", SqlDbType.NVarChar, 50).Value = textBox11.Text.ToString();
                        sqlcmx.Parameters.Add("@GiaVe", SqlDbType.Float, 50).Value = Convert.ToDouble(comboBox15.Text);
                        sqlcmx.Parameters.Add("@TimeMua", SqlDbType.DateTime).Value = textBox9.Text;
                        sqlcmx.Parameters.Add("@SLVe", SqlDbType.Int, 50).Value = Convert.ToInt32(comboBox11.Text);
                        sqlcmx.Parameters.Add("@GhiChu", SqlDbType.NVarChar, 50).Value = abx;
                        sqlcmx.ExecuteNonQuery();
                        sqlcmx.Dispose();

                        MessageBox.Show("Đặt Vé Thành Công.", "Thông Báo");

                        //Cập nhật lại tình trạng ghế sau khi bán xong vé
                        //int slve = Convert.ToInt32(comboBox11.Text);
                        if (slve == 1)
                        {
                            Connectdatabase a2 = new Connectdatabase();
                            SqlCommand sqlcm22 = a2.connectdatabase("update ThongTinGhe set TinhTrang = 'No' Where  MaTau = '" + textBox7.Text + "' and MaChuyen = '" + comboBox4.Text + "' and MaToa = '" + comboBox5.Text + "'  and STTToa='" + comboBox7.Text + "' and STTGhe='" + comboBox8.Text + "'  ");
                            sqlcm22.ExecuteNonQuery();
                        }
                        else
                        {
                            int soghe = Convert.ToInt32(comboBox8.Text);
                            string ghe = comboBox8.Text;
                            string vitrighe = null;
                            for (int i = 0; i < slve; i++)
                            {


                                Connectdatabase a3 = new Connectdatabase();
                                SqlCommand sqlcm23 = a3.connectdatabase("update ThongTinGhe set TinhTrang = 'No' Where  MaTau = '" + textBox7.Text + "' and MaChuyen = '" + comboBox4.Text + "' and MaToa = '" + comboBox5.Text + "'  and STTToa='" + comboBox7.Text + "' and STTGhe = '" + soghe + "' ");
                                sqlcm23.ExecuteNonQuery();
                                vitrighe = vitrighe + " " + soghe;
                                soghe++;

                            }
                            Connectdatabase s = new Connectdatabase();
                            SqlCommand sqlcms = s.connectdatabase("update BienLai set GhiChu = '" + vitrighe + "' where GhiChu = 'N'");
                            sqlcms.ExecuteNonQuery();
                        }

                        // Cập nhật lại số lượng ghế trong toa đó
                        Connectdatabase z = new Connectdatabase();
                        SqlCommand sqlcmz = z.connectdatabase("select GheConLai from ThongTinToa Where  MaTau = '" + textBox7.Text + "' and MaChuyen = '" + comboBox4.Text + "' and MaToa = '" + comboBox5.Text + "'  and STTToa='" + comboBox7.Text + "'");
                        string gheconlai = null;// lấy giá trị trong bảng ra
                        gheconlai = sqlcmz.ExecuteScalar().ToString();
                        sqlcmz.Dispose();
                        int gheconlai2 = Convert.ToInt32(gheconlai);
                        int soghemoi = 0;
                        int slv = Convert.ToInt32(comboBox11.Text);
                        soghemoi = gheconlai2 - slv;
                        textBox8.Text = soghemoi.ToString();
                        Connectdatabase z1 = new Connectdatabase();
                        SqlCommand sqlcmz1 = z1.connectdatabase("update ThongTinToa set GheConLai = '" + textBox8.Text + "' Where  MaTau = '" + textBox7.Text + "' and MaChuyen = '" + comboBox4.Text + "' and MaToa = '" + comboBox5.Text + "'  and STTToa='" + comboBox7.Text + "' ");
                        sqlcmz1.ExecuteNonQuery();


                        dataGridView1.Visible = false;
                        dataGridView2.Visible = false;
                        dataGridView5.Visible = false;
                        dataGridView3.Visible = true;

                        //  Xem lại danh sách biên lai
                        Connectdatabase a1 = new Connectdatabase();
                        SqlCommand sqlcm21 = a1.connectdatabase("SELECT * FROM BIENLAI ");

                        SqlDataAdapter sqlcm31 = new SqlDataAdapter(sqlcm21);
                        DataTable data31 = new DataTable();
                        sqlcm31.Fill(data31);
                        dataGridView3.DataSource = data31;
                    }
                }
                else
                {
                    MessageBox.Show("Lựa chọn các điều kiện cần thiết.");
                }
            }
            catch (Exception m)
            {
                MessageBox.Show(m.Message + "Vé chưa được bán..");
                return;
            }

        }

        // Tính luôn giá vé mỗi khi lựa chọn combox Loại Toa
        private void comboBox5_SelectedIndexChanged(object sender, EventArgs e)
        {
            //  Tính giá vé dùng proc
            SqlConnection a1 = new SqlConnection(connect);
            a1.Open();

            string sqlcomman = "USP_TINHTIENVE";
            SqlCommand sqlcm21 = new SqlCommand(sqlcomman, a1);
            sqlcm21.CommandType = CommandType.StoredProcedure;
            sqlcm21.Parameters.Add("@DIADIEMDI", SqlDbType.NVarChar, 50).Value = comboBox1.Text.ToString();
            sqlcm21.Parameters.Add("@SOLUONG", SqlDbType.Int, 50).Value = Convert.ToInt32(comboBox11.Text);
            sqlcm21.Parameters.Add("@LOAICHUYEN", SqlDbType.NVarChar, 50).Value = comboBox6.Text.ToString();
            sqlcm21.Parameters.Add("@LOAIGHE", SqlDbType.NVarChar, 50).Value = comboBox5.Text.ToString();
            sqlcm21.Parameters.Add("@MADOITUONG", SqlDbType.NVarChar, 50).Value = textBox11.Text.ToString();
            //sqlcm21.ExecuteNonQuery();
            sqlcm21.Dispose();

            SqlDataAdapter x = new SqlDataAdapter(sqlcm21);
            DataTable datax = new DataTable();
            x.Fill(datax);
            comboBox15.DataSource = datax;
            comboBox15.DisplayMember = "GIAIN";
            comboBox15.ValueMember = "GIAIN";
        }

       
        

        private void comboBox11_SelectedIndexChanged(object sender, EventArgs e)
        {
            //  Tính giá vé dùng proc
            SqlConnection a1 = new SqlConnection(connect);
            a1.Open();

            string sqlcomman = "USP_TINHTIENVE";
            SqlCommand sqlcm21 = new SqlCommand(sqlcomman, a1);
            sqlcm21.CommandType = CommandType.StoredProcedure;
            sqlcm21.Parameters.Add("@DIADIEMDI", SqlDbType.NVarChar, 50).Value = comboBox1.Text.ToString();
            sqlcm21.Parameters.Add("@SOLUONG", SqlDbType.Int, 50).Value = Convert.ToInt32(comboBox11.Text);
            sqlcm21.Parameters.Add("@LOAICHUYEN", SqlDbType.NVarChar, 50).Value = comboBox6.Text.ToString();
            sqlcm21.Parameters.Add("@LOAIGHE", SqlDbType.NVarChar, 50).Value = comboBox5.Text.ToString();
            sqlcm21.Parameters.Add("@MADOITUONG", SqlDbType.NVarChar, 50).Value = textBox11.Text.ToString();
       //     sqlcm21.ExecuteNonQuery();
            sqlcm21.Dispose();

            SqlDataAdapter x = new SqlDataAdapter(sqlcm21);
            DataTable datax = new DataTable();
            x.Fill(datax);
            comboBox15.DataSource = datax;
            comboBox15.DisplayMember = "GIAIN";
            comboBox15.ValueMember = "GIAIN";
            
        }

       

        // tính luôn giá vé khi thay đôi comboBox
        private void comboBox5_SelectedIndexChanged_1(object sender, EventArgs e)
        {

            //  Tính giá vé dùng proc
            SqlConnection a1 = new SqlConnection(connect);
            a1.Open();

            string sqlcomman = "USP_TINHTIENVE";
            SqlCommand sqlcm21 = new SqlCommand(sqlcomman, a1);
            sqlcm21.CommandType = CommandType.StoredProcedure;
            sqlcm21.Parameters.Add("@DIADIEMDI", SqlDbType.NVarChar, 50).Value = comboBox1.Text.ToString();
            sqlcm21.Parameters.Add("@SOLUONG", SqlDbType.Int, 50).Value = Convert.ToInt32(comboBox11.Text);
            sqlcm21.Parameters.Add("@LOAICHUYEN", SqlDbType.NVarChar, 50).Value = comboBox6.Text.ToString();
            sqlcm21.Parameters.Add("@LOAIGHE", SqlDbType.NVarChar, 50).Value = comboBox5.Text.ToString();
            sqlcm21.Parameters.Add("@MADOITUONG", SqlDbType.NVarChar, 50).Value = textBox11.Text.ToString();
    //        sqlcm21.ExecuteNonQuery();
            sqlcm21.Dispose();

            SqlDataAdapter x = new SqlDataAdapter(sqlcm21);
            DataTable datax = new DataTable();
            x.Fill(datax);
            comboBox15.DataSource = datax;
            comboBox15.DisplayMember = "GIAIN";
            comboBox15.ValueMember = "GIAIN";
        }


        // Tính luôn giá vé mỗi khi lựa chọn combox Loại Chuyến
        private void comboBox6_SelectedIndexChanged(object sender, EventArgs e)
        {
            //  Tính giá vé dùng proc
            SqlConnection a1 = new SqlConnection(connect);
            a1.Open();

            string sqlcomman = "USP_TINHTIENVE";
            SqlCommand sqlcm21 = new SqlCommand(sqlcomman, a1);
            sqlcm21.CommandType = CommandType.StoredProcedure;
            sqlcm21.Parameters.Add("@DIADIEMDI", SqlDbType.NVarChar, 50).Value = comboBox1.Text.ToString();
            sqlcm21.Parameters.Add("@SOLUONG", SqlDbType.Int, 50).Value = Convert.ToInt32(comboBox11.Text);
            sqlcm21.Parameters.Add("@LOAICHUYEN", SqlDbType.NVarChar, 50).Value = comboBox6.Text.ToString();
            sqlcm21.Parameters.Add("@LOAIGHE", SqlDbType.NVarChar, 50).Value = comboBox5.Text.ToString();
            sqlcm21.Parameters.Add("@MADOITUONG", SqlDbType.NVarChar, 50).Value = textBox11.Text.ToString();
           // sqlcm21.ExecuteNonQuery();
            sqlcm21.Dispose();

            SqlDataAdapter x = new SqlDataAdapter(sqlcm21);
            DataTable datax = new DataTable();
            x.Fill(datax);
            comboBox15.DataSource = datax;
            comboBox15.DisplayMember = "GIAIN";
            comboBox15.ValueMember = "GIAIN";
        }

       

        private void textBox9_TextChanged(object sender, EventArgs e)
        {
         //   Timer1.Start();
        }





        // up date thông tin hành khách

        private void button9_Click(object sender, EventArgs e)
        {

            if (comboBox13.Text != "" && textBox2.Text != "" && comboBox2.Text != "" && textBox4.Text != "" && textBox6.Text != "" && textBox5.Text != "" && comboBox11.Text != "")
            {
                dataGridView1.Visible = false;
                dataGridView2.Visible = true;
                dataGridView3.Visible = false;
                dataGridView4.Visible = false;
                dataGridView5.Visible = false;

                SqlConnection a = new SqlConnection(connect);
                a.Open();

                string sqlcomman = "USP_UPDATEINFOKHACHHANG";
                SqlCommand sqlcm2 = new SqlCommand(sqlcomman, a);
                sqlcm2.CommandType = CommandType.StoredProcedure;
                sqlcm2.Parameters.Add("@MAHK", SqlDbType.NVarChar, 50).Value = comboBox13.Text.ToString();
                sqlcm2.Parameters.Add("@HOTEN", SqlDbType.NVarChar, 50).Value = textBox2.Text.ToString();
                sqlcm2.Parameters.Add("@GIOITINH", SqlDbType.NVarChar, 50).Value = comboBox2.Text.ToString();
                sqlcm2.Parameters.Add("@CMND", SqlDbType.NVarChar, 50).Value = textBox4.Text.ToString();
                sqlcm2.Parameters.Add("@DIACHI", SqlDbType.NVarChar, 50).Value = textBox6.Text.ToString();
                sqlcm2.Parameters.Add("@DIENTHOAI", SqlDbType.NVarChar, 50).Value = textBox5.Text.ToString();
                sqlcm2.Parameters.Add("@SOLUONG", SqlDbType.Int, 50).Value = Convert.ToInt32(comboBox11.Text);
                sqlcm2.ExecuteNonQuery();
                sqlcm2.Dispose();
                MessageBox.Show("Thông Tin quý khách đã được lưu.");


                Connectdatabase a1 = new Connectdatabase();
                SqlCommand sqlcmx = a1.connectdatabase("SELECT * FROM HANHKHACH ");

                SqlDataAdapter sqlcm31 = new SqlDataAdapter(sqlcmx);
                DataTable data3 = new DataTable();
                sqlcm31.Fill(data3);
                dataGridView2.DataSource = data3;
            }
            else
            {
                MessageBox.Show("Điền Thông Tin Cần Update .");
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
        
            textBox9.Text = DateTime.Now.ToString();
          //  string ketqua = label14.Text + DateTime.Parse(3).Replace("/", "");
        }

        private void button10_Click(object sender, EventArgs e)
        {
                dataGridView1.Visible = false;
                dataGridView3.Visible = false;
                dataGridView2.Visible = false;
                dataGridView4.Visible = false;
                dataGridView5.Visible = false;
                textBox1.Visible = true;

                if (comboBox16.Text == "Chuyến Tàu")
                {
                    try
                    {
                        dataGridView4.Visible = true;
                        SqlConnection a1 = new SqlConnection(connect);
                        a1.Open();
                        string sqlcomman = "DEMCHUYENTAU";
                        SqlCommand sqlcm21 = new SqlCommand(sqlcomman, a1);
                        sqlcm21.CommandType = CommandType.StoredProcedure;
                        sqlcm21.Parameters.Add("@NOIDEN", SqlDbType.NVarChar, 50).Value = comboBox1.Text.ToString();
                        //sqlcm21.ExecuteNonQuery();
                        sqlcm21.Dispose();
                        SqlDataAdapter x = new SqlDataAdapter(sqlcm21);
                        DataTable datax = new DataTable();
                        x.Fill(datax);
                        dataGridView4.DataSource = datax;

                        //textBox13.Text = "Chuyến Tàu.";
                        string a = dataGridView4.Rows[1].Cells[2].Value.ToString();
                        //  textBox10.Text = dataGridView4.Rows[1].Cells[2].Value.ToString();
                        textBox13.Text = "Có tổng cộng " + a + " Chuyến Tàu.";
                    }
                    catch 
                    {
                        dataGridView4.Visible = false;
                        textBox13.Text = "Không Có Chuyến tàu !";
                    }

                }
                
                if (comboBox16.Text == "Danh Sách Biên Lai")
                {
                    try
                    {
                        dataGridView4.Visible = true;
                        SqlConnection a = new SqlConnection(connect);
                        a.Open();

                        string sqlcomman = "DEMBIENLAI";
                        SqlCommand sqlcm2 = new SqlCommand(sqlcomman, a);
                        // sqlcm2.ExecuteNonQuery();
                        sqlcm2.Dispose();
                        SqlDataAdapter y = new SqlDataAdapter(sqlcm2);
                        DataTable datay = new DataTable();
                        y.Fill(datay);
                        dataGridView4.DataSource = datay;
                        string b = dataGridView4.Rows[0].Cells[1].Value.ToString();
                        textBox13.Text = "Có tổng cộng" + b + " Biên Lai";
                    }
                    catch
                    {
                        dataGridView4.Visible = false;
                        textBox13.Text = "Không Có Biên Lai!";
                    }
                }
            }
           
      

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            if (comboBox1.Text != "")
            {         
                    dataGridView1.Visible = true;
                    dataGridView2.Visible = false;
                    dataGridView3.Visible = false;
                    dataGridView4.Visible =false;
                    dataGridView5.Visible = false;
                    string date = Convert.ToString(dateTimePicker1.Value.Day);
                    string month = Convert.ToString(dateTimePicker1.Value.Month);
                    string year = Convert.ToString(dateTimePicker1.Value.Year);

                    string date1 = dateTimePicker1.Value.ToString("dd/MM/yyyy");
                    Connectdatabase a = new Connectdatabase();
                    SqlCommand sqlcm2 = a.connectdatabase("SELECT MaTau as Mã_Tàu,DDDenTram as Điểm_Đến , MaChuyen as Mã_Chuyến ,TGBatDauDi as Giờ_Đi, TGDenTram as Giờ_Đến FROM DSThoiGianTGTQ t WHERE    t.DDDenTram = N'" + comboBox1.Text + "' ");
                    SqlDataAdapter sqlcm3 = new SqlDataAdapter(sqlcm2);
                    DataTable data3 = new DataTable();
                    sqlcm3.Fill(data3);
                    dataGridView1.DataSource = data3;     
            }
            else
            {
                MessageBox.Show("Vui lòng chọn nơi đến. ");
            }
            // Xem luôn giá vé bang proc
            SqlConnection a2 = new SqlConnection(connect);
            a2.Open();

            string sqlcomman2 = "USP_DOCGIAVEDETINHTIEN";
            SqlCommand sqlcm22 = new SqlCommand(sqlcomman2, a2);
            sqlcm22.CommandType = CommandType.StoredProcedure;
            sqlcm22.Parameters.Add("@MOTAGA", SqlDbType.NVarChar, 50).Value = comboBox1.Text.ToString();
            //sqlcm22.ExecuteNonQuery();
            sqlcm22.Dispose();
            SqlDataAdapter x = new SqlDataAdapter(sqlcm22);
            DataTable datax = new DataTable();
            x.Fill(datax);
            comboBox14.DataSource = datax;
            comboBox14.DisplayMember = "GiaVe";
            comboBox14.ValueMember = "GiaVe";
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            dataGridView1.Visible = false;
            dataGridView2.Visible = false;
            dataGridView3.Visible = false;
            dataGridView4.Visible = false;
            dataGridView4.Visible = true;
            SqlConnection a1 = new SqlConnection(connect);
            a1.Open();
            string sqlcomman = "SHOWTHONGTINCHUYENTAUDI_INNGAY";
            SqlCommand sqlcm21 = new SqlCommand(sqlcomman, a1);
            sqlcm21.CommandType = CommandType.StoredProcedure;
            sqlcm21.Parameters.Add("@DATETIME", SqlDbType.DateTime).Value = dateTimePicker1.Value;
          //  sqlcm21.ExecuteNonQuery();
            sqlcm21.Dispose();
            SqlDataAdapter x1 = new SqlDataAdapter(sqlcm21);
            DataTable datax1 = new DataTable();
            x1.Fill(datax1);
            dataGridView4.DataSource = datax1;
        }

        //private void comboBox17_SelectedIndexChanged(object sender, EventArgs e)
        //{
            
        //    Connectdatabase a19 = new Connectdatabase();
        //    SqlCommand sqlcm = a19.connectdatabase("Select MaTau from BienLai where MaVe = N'" + comboBox17.Text + "'");
        //    SqlCommand sqlcm1 = a19.connectdatabase("Select MaLC from BienLai where MaVe = N'" + comboBox17.Text + "'");
        //    SqlCommand sqlcm2 = a19.connectdatabase("Select HoTenHK from BienLai where MaVe = N'" + comboBox17.Text + "'");
        //    SqlCommand sqlcm3 = a19.connectdatabase("Select DiaDiemDen from BienLai where MaVe = N'" + comboBox17.Text + "'");
        //    SqlCommand sqlcm4 = a19.connectdatabase("Select ThoiGianMuaVe from BienLai where MaVe = N'" + comboBox17.Text + "'");
        //    SqlCommand sqlcm5 = a19.connectdatabase("Select GioDi from BienLai where MaVe = N'" + comboBox17.Text + "'");
        //    SqlCommand sqlcm6 = a19.connectdatabase("Select MaToa from BienLai where MaVe = N'" + comboBox17.Text + "'");
           
        //    SqlCommand sqlcm8 = a19.connectdatabase("Select STTGhe from BienLai where MaVe = N'" + comboBox17.Text + "'");
        //    SqlCommand sqlcm9 = a19.connectdatabase("Select DiaDiemDi from BienLai where MaVe = N'" + comboBox17.Text + "'");
        //    SqlCommand sqlcm10 = a19.connectdatabase("Select MaDT from BienLai where MaVe = N'" + comboBox17.Text + "'");
        //   // SqlCommand sqlcm11 = a19.connectdatabase("Select GiaVE from BienLai where MaVe = N'" + comboBox17.Text + "'");
        //    SqlCommand sqlcm12 = a19.connectdatabase("Select MaHKhach from BienLai where MaVe = N'" + comboBox17.Text + "'");
        //    SqlCommand sqlcm13 = a19.connectdatabase("Select  MaChuyen from BienLai where MaVe = N'" + comboBox17.Text + "'");
        //    string MT = null;
        //    string LC = null;
        //    string Ten = null;
        //    string DDD = null;
        //    string TimeMua = null;
        //    string GioDi = null;
        //    string MaToa = null;
        //    string DiaChi = null;
        //    string STTGhe = null;
        //    string DDi = null;
        //    string MaDT = null;
        //  //  string GiaVe = null;
        //    string MaHK = null;
        //    string MaChuyen = null;
        //    string CMND= null;


        //    MT = sqlcm.ExecuteScalar().ToString();
        //    LC = sqlcm1.ExecuteScalar().ToString();
        //    Ten = sqlcm2.ExecuteScalar().ToString();
        //    DDD = sqlcm3.ExecuteScalar().ToString();
        //    TimeMua = sqlcm4.ExecuteScalar().ToString();
        //    GioDi = sqlcm5.ExecuteScalar().ToString();
        //    MaToa = sqlcm6.ExecuteScalar().ToString();
        //    STTGhe = sqlcm8.ExecuteScalar().ToString();
        //    DDi = sqlcm9.ExecuteScalar().ToString();
        //    MaDT = sqlcm10.ExecuteScalar().ToString();
        // //   GiaVe = sqlcm11.ExecuteScalar().ToString();            
        //    MaChuyen = sqlcm13.ExecuteScalar().ToString();
        //    MaHK = sqlcm12.ExecuteScalar().ToString();
        //     sqlcm.Dispose();

        //    textBox7.Text = MT;
        //    comboBox6.Text = LC;
        //    textBox2.Text = Ten;
        //    comboBox1.Text = DDD;
        //    textBox12.Text = TimeMua;
        //    comboBox9.Text = GioDi;
        //    comboBox5.Text = MaToa;
        //    comboBox8.Text = STTGhe;
        //    comboBox1.Text = DDi;
        //    textBox11.Text = MaDT;
        // //   comboBox15.Text = GiaVe;
        //    comboBox4.Text = MaChuyen;
        //    comboBox13.Text = MaHK;

        //     Connectdatabase a20 = new Connectdatabase();
        //     SqlCommand sqlcm7 = a20.connectdatabase("Select DiaChi from HanhKhach where MaHKhach = N'" + comboBox13.Text + "'");
        //     SqlCommand sqlcm14 = a20.connectdatabase("Select CMND from HanhKhach where MaHKhach = N'" + comboBox13.Text + "'");
        //     DiaChi = sqlcm7.ExecuteScalar().ToString();
        //     CMND = sqlcm14.ExecuteScalar().ToString();
            
        //     sqlcm.Dispose();
        //     textBox4.Text = CMND;
        //     textBox6.Text = DiaChi;


        //}

        private void button1_Click(object sender, EventArgs e)
        {
            dataGridView1.Visible = false;
            dataGridView3.Visible = false;
            dataGridView2.Visible = false;
            dataGridView4.Visible = false;
            dataGridView5.Visible = true;
            //  Xem lại danh sách trả vé
            Connectdatabase a1 = new Connectdatabase();
            SqlCommand sqlcm21 = a1.connectdatabase("SELECT * FROM DOIHOACTRAVE ");

            SqlDataAdapter sqlcm31 = new SqlDataAdapter(sqlcm21);
            DataTable data31 = new DataTable();
            sqlcm31.Fill(data31);
            dataGridView5.DataSource = data31;


        }



        // Tự động cập nhật số thứ tự ghế
        private void comboBox7_SelectedIndexChanged(object sender, EventArgs e)
        {
            Connectdatabase a3 = new Connectdatabase();
            SqlCommand sqlcm23 = a3.connectdatabase("select * from ThongTinGhe where MaTau = '" + textBox7.Text + "' and MaChuyen = '" + comboBox4.Text + "' and MaToa = '" + comboBox5.Text + "'  and STTToa='" + comboBox7.Text + "'and TinhTrang ='Yes'");
            SqlDataAdapter sqldt23 = new SqlDataAdapter(sqlcm23);
            DataTable data33 = new DataTable();
            sqldt23.Fill(data33);
            comboBox8.DataSource = data33;
            comboBox8.DisplayMember = "STTGhe";
            comboBox8.ValueMember = "STTGhe";


            // Load lên tổng số ghê chuyên tàu đó
            Connectdatabase z = new Connectdatabase();
            SqlCommand sqlcmz = z.connectdatabase("select GheConLai from ThongTinToa Where  MaTau = '" + textBox7.Text + "' and MaChuyen = '" + comboBox4.Text + "' and MaToa = '" + comboBox5.Text + "'  and STTToa='" + comboBox7.Text + "'");
            string gheconlai;//= null;// lấy giá trị trong bảng ra
            gheconlai = sqlcmz.ExecuteScalar().ToString();
            sqlcmz.Dispose();
            int gheconlai2 = Convert.ToInt32(gheconlai);
            textBox8.Text = gheconlai2.ToString();




        }

        private void Ban_Ve_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void dataGridView4_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            textBox7.Text = dataGridView4.Rows[e.RowIndex].Cells[0].Value.ToString();
            comboBox4.Text = dataGridView4.Rows[e.RowIndex].Cells[1].Value.ToString();
            comboBox9.Text = dataGridView4.Rows[e.RowIndex].Cells[3].Value.ToString();
            if (comboBox11.Text == "")
            {
                MessageBox.Show("Chọn Số lượng vé cần đặt");
                  return;
            }

            Connectdatabase a2 = new Connectdatabase();
            SqlCommand sqlcm22 = a2.connectdatabase("select * from ThongTinChuyenTau where MaTau = '" + textBox7.Text + "' and MaChuyen ='" + comboBox4.Text + "'");
            SqlDataAdapter sqldt22 = new SqlDataAdapter(sqlcm22);
            DataTable data30 = new DataTable();
            sqldt22.Fill(data30);
            comboBox6.DataSource = data30;
            comboBox6.DisplayMember = "MaLC";
            comboBox6.ValueMember = "MaLC";

            Connectdatabase a4 = new Connectdatabase();
            SqlCommand sqlcm24 = a4.connectdatabase("select * from ThongTinToa where MaTau = '" + textBox7.Text + "' and MaChuyen ='" + comboBox4.Text + "'");
            SqlDataAdapter sqldt24 = new SqlDataAdapter(sqlcm24);
            DataTable data32 = new DataTable();
            sqldt24.Fill(data32);
            comboBox5.DataSource = data32;
            comboBox5.DisplayMember = "MaToa";
            comboBox5.ValueMember = "MaToa";

            comboBox7.DataSource = data32;
            comboBox7.DisplayMember = "STTToa";
            comboBox7.ValueMember = "STTToa";

            Connectdatabase a3 = new Connectdatabase();
            SqlCommand sqlcm23 = a3.connectdatabase("select * from ThongTinGhe where MaTau = '" + textBox7.Text + "' and MaChuyen = '" + comboBox4.Text + "' and MaToa = '" + comboBox5.Text + "'  and STTToa='" + comboBox7.Text + "'and TinhTrang ='Yes'");
            SqlDataAdapter sqldt23 = new SqlDataAdapter(sqlcm23);
            DataTable data33 = new DataTable();
            sqldt23.Fill(data33);
            comboBox8.DataSource = data33;
            comboBox8.DisplayMember = "STTGhe";
            comboBox8.ValueMember = "STTGhe";
        }


       


        



   

    }
   
}
